package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/go-redis/redis"
)

type RedirectController struct {
	beego.Controller
}

func (c * RedirectController)  Get(){
	client := redis.NewClient(&redis.Options{
		Network:            "tcp",
		Addr:               "127.0.0.1:6379",
		Dialer:             nil,
		OnConnect:          nil,
		Password:           "",
		DB:                 1,
		MaxRetries:         0,
		MinRetryBackoff:    0,
		MaxRetryBackoff:    0,
		DialTimeout:        0,
		ReadTimeout:        0,
		WriteTimeout:       0,
		PoolSize:           0,
		MinIdleConns:       0,
		MaxConnAge:         0,
		PoolTimeout:        0,
		IdleTimeout:        0,
		IdleCheckFrequency: 0,
		TLSConfig:          nil,
	})
	//conn, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialConnectTimeout(2*time.Second))
	_, err := client.Ping().Result()
	if err != nil {
		logs.Info("redis error")
		c.StopRun()
	}
	url := c.Ctx.Request.URL.Path[1:]
	url,err = client.HGet("data",url).Result()
	if err != nil {
		c.TplName="404.tpl"
	}else{
		c.Redirect(url,302)
	}
}